package com.example.deepseek.demos.web;

import com.alibaba.fastjson2.JSON;
import okhttp3.*;

import java.io.IOException;
import java.net.SocketTimeoutException;
import java.util.List;
import java.util.concurrent.TimeUnit;


public class DeepSeekClient {
    private static final String url="https://api.deepseek.com/chat/completions";
    private static final String api = "sk-71b0eb92539e4a6588970a71274b62c4";

    public static String send(String content) throws IOException {

        DeepSeekRequest deepSeekRequest = new DeepSeekRequest();
        DeepSeekRequest.Message message = new DeepSeekRequest.Message();
        message.setRole("user");
        message.setContent(content + ",回答内容不超过200个字");
        deepSeekRequest.getMessages().add(message);
        deepSeekRequest.getMessages().add(DeepSeekRequest.systemMessage);
        String json = JSON.toJSONString(deepSeekRequest);

        OkHttpClient client = new OkHttpClient().newBuilder()
                .connectTimeout(30, TimeUnit.SECONDS)
                .readTimeout(60, TimeUnit.SECONDS)
                .writeTimeout(30, TimeUnit.SECONDS)
                .build();

        MediaType mediaType = MediaType.parse("application/json");
        RequestBody body = RequestBody.create(mediaType, json);
        Request request = new Request.Builder()
                .url(url)
                .method("POST", body)
                .addHeader("Content-Type", "application/json")
                .addHeader("Accept", "application/json")
                .addHeader("Authorization", "Bearer " + api)
                .build();

        try (Response response = client.newCall(request).execute()) {
            if (response.isSuccessful()) {
                String responseBody = response.body().string();
                DeepSeekResponse deepSeekResponse = JSON.parseObject(responseBody, DeepSeekResponse.class);
                if (deepSeekResponse.getChoices() != null && !deepSeekResponse.getChoices().isEmpty()) {
                    DeepSeekResponse.Message answer = deepSeekResponse.getChoices().get(0).getMessage();
                    return answer.getContent();
                } else {
                    return "未收到有效响应";
                }
            } else {
                String errorBody = response.body().string();
                System.out.println("API 请求失败，状态码: " + response.code() + ", 响应: " + errorBody);
                return "请求失败，状态码: " + response.code();
            }
        } catch (SocketTimeoutException e) {
            System.out.println("请求超时: " + e.getMessage());
            return "请求超时，请稍后重试";
        } catch (IOException e) {
            System.out.println("网络异常: " + e.getMessage());
            return "网络异常，请检查连接";
        } catch (Exception e) {
            System.out.println("其他异常: " + e.getMessage());
            return "系统异常，请重新尝试";
        }
    }

    public static void main(String[] args) {
        try {
            String send = DeepSeekClient.send("你是谁？");
            System.out.println(send);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
